﻿using DelonixRegia.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DelonixRegia
{
    public partial class AccountManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Account a = (Account)Session["acc"];

            if (a != null)
            {
                if (a.userType < 3) 
                    Response.Redirect("Default.aspx");
            } else
                Response.Redirect("SignIn.aspx");
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(tbxUsername.Text)) && (!string.IsNullOrEmpty(tbxPassword.Text))
                && (!string.IsNullOrEmpty(tbxPasswordAgn.Text)) && (!string.IsNullOrEmpty(tbxEmail.Text))
                && (!string.IsNullOrEmpty(tbxContact.Text)) && (!ddlDuty.SelectedItem.Text.Equals("Not Selected")))
            {
                if (tbxPassword.Text.Equals(tbxPasswordAgn.Text))
                {
                    if (!AccountManagementDBManager.checkDuplicate(tbxUsername.Text, tbxEmail.Text))
                    {
                        if (AccountManagementDBManager.createAccount(tbxUsername.Text, tbxPassword.Text, tbxContact.Text, tbxEmail.Text, int.Parse(ddlDuty.SelectedItem.Value)))
                        {
                            ClientScriptMgr.showSuccessBox("Account has been successfully created!");
                        }
                        else
                            ClientScriptMgr.showErrorBox("Unable to create account, pls try again later!");
                    }
                    else
                        ClientScriptMgr.showErrorBox("Username or Email Address already exists!");
                }
                else
                    ClientScriptMgr.showErrorBox("Passwords do not match!");
            }
            else
                ClientScriptMgr.showErrorBox("One or more required fields are empty!");
        }
    }
}